import java.util.Scanner;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.io.IOException;
import java.io.FileNotFoundException;

public class Menu 
{

	private MasterRecDatabase masterRecDatabase;
	private RecListManager recListManager;
	private UserDatabase userDatabase;	
	private User user;
	
	private String browser;
	private String admin;
	private String editor;
	private String verifier;
	
	private String masterRecDatabaseDir;
	private String recListDir;
	private String unloadedListDir;
	private String userDatabaseDir;
	
	/**
	 * Initializes and starts the menu.
	 */
	public Menu()
	{
		browser = "browser";
		admin = "admin";
		editor = "editor";
		verifier = "verifier";	
		
		masterRecDatabaseDir = "IMDB/MasterRecDatabase";
		recListDir = "IMDB/RecLists";
		unloadedListDir = "IMDB/UnloadedLists";
		userDatabaseDir = "IMDB/UserDatabase/UserDatabase.dat";
		
		masterRecDatabase = new MasterRecDatabase(masterRecDatabaseDir);
		recListManager = new RecListManager(recListDir, unloadedListDir);
		userDatabase = new UserDatabase(userDatabaseDir);
		user = null;
		load();		
		login();
	}
	
	/**
	 * Loads the UserDatabase, all RecLists and all RecDatabases into the system.
	 */
	private void load()
	{
		try
		{
			masterRecDatabase.load();
		}
		catch (IOException exception)
		{
			System.out.println("There was an error loading the Master Recommendation Database from the previous session.");
		}
			
		try
		{
			recListManager.load();
		}
		catch (IOException exception)
		{
			System.out.println("There was an error loading the Rec Lists from the previous session.");
		}
			
		try
		{
			userDatabase.load();
		}
		catch (IOException exception)
		{
			System.out.println("There was an error loading the User Database.");
		}
	}
	
	/**
	 * Gets and checkers a user's credentials and assigns them the corresponding user object.
	 * If the userDatabase is empty, this method creates an admin user type with the user's input.
	 */
	public void login()
	{
		Scanner in = new Scanner(System.in);
		String enteredUserName;
		String enteredPassword;
		
		
		if (userDatabase.isEmpty())
		{
			System.out.println("There are currently no users in the system. As the first you will be assigned an administrator position.");
			System.out.println("Please enter your desired user name:");
			enteredUserName = in.nextLine();
			System.out.println("Please enter your desired password:");
			enteredPassword = in.nextLine();
			try
			{
				userDatabase.addUser(enteredUserName, enteredPassword, admin);
				try
				{
					userDatabase.update();
				}
				catch (FileNotFoundException exception)
				{
					System.out.println("There was an error adding your account to the system.");
					System.exit(0);
				}
				user = userDatabase.getUser(enteredUserName);
			}
			catch (UserNameTakenException exception){}
			catch (NoSuchUserException exception){}
			
			run();
			return;			
		}
		
		while (true)
		{
			System.out.println("Please enter your user name:");
			enteredUserName = in.next();
			System.out.println("Please enter your password:");
			enteredPassword = in.next();
			if (userDatabase.verify(enteredUserName, enteredPassword))
			{
				try
				{
					user = userDatabase.getUser(enteredUserName);
				}
				catch (NoSuchUserException exception) { System.out.println("Somehow this NoSuchUserException was thrown."); }
				if (user.getType().equals(browser))
					user.loadPRecList("IMDB/PersonalRecLists");
				run();
				System.exit(0);
			}
			else
			{
				while (true)
				{
					System.out.println("The entered credentials are not valid. Do you want to quit? y/n");
					if (in.nextLine().equalsIgnoreCase("y"))
						System.exit(0);
					else if (in.nextLine().equalsIgnoreCase("n"))
						break;
					else
						System.out.println("Invalid command entered.");
				}
			}
		}
	}
	
	/**
	 * Gets and carries out a user's commands.
	 */
	public void run()
	{
		System.out.println("Welcome to the IMDB system. Enter 'help' for a list of available commands.");
		while(true)
		{
			Scanner in = new Scanner(System.in);
			String[] input = in.nextLine().split(" ");
			
			if (input.length == 1 && input[0].equals("help"))
			{
				if (user.getType().equals(browser))
				{
					System.out.println("Available Commands \nhelp\ndisplayplist \nlistdbs \ndisplaydb DB-NAME \ndisplaydb sorted DB-NAME \nsearchdb DB-NAME for title TITLE YEAR \nsearchdb DB-NAME for genre GENRE-PHRASE (split multiple genres with \" / \") \nexit");
				}
				else if (user.getType().equals(admin))
				{
					System.out.println("Available Commands \nhelp \nadduser {\"editor\",\"verifier\",\"browser\",\"admin\"} USER-NAME USER-PASSWORD \ndeluser USER-NAME \nexit");
				}
				else if (user.getType().equals(editor))
				{
					System.out.println("Available Commands \nhelp \nloadrec LIST-FILE as LIST-NAME \nlistrecs \neditrec LIST-NAME \ndisplayrec LIST-NAME \ndisplayrec sorted LIST-NAME \nsearchrec LIST-NAME for title TITLE YEAR \nsearchrec LIST-NAME for genre GENRE-PHRASE (split multiple genres with \" / \") \nviewhistoryrec LIST-NAME \nexit");					
				}
				else if (user.getType().equals(verifier))
				{
					System.out.println("Available Commands \nhelp \nmergerec LIST-NAME with DB-NAME \nlistrecs \nlistdbs \ndisplayrec LIST-NAME \ndisplayrec sorted LIST-NAME \ndisplaydb DB-NAME \ndisplaydb sorted DB-NAME \nverifyrec LIST-NAME \nviewhistorydb DB-NAME \nviewhistoryrec LIST-NAME \nsearchrec LIST-NAME for title TITLE YEAR \nsearchrec LIST-NAME for genre GENRE-PHRASE (split multiple genres with \" / \") \nexit");
				}
			}
			else if (input.length == 4 && input[0].equals("loadrec"))
			{
				if (user.getType().equals(editor))
				{
					try
					{
						recListManager.load(input[1], input[3], user.getName());
						try
						{
							recListManager.getRecList(input[3]).addHistory("This list was loaded into the system.", user.getName());
							recListManager.getRecList(input[3]).update(recListDir);
							System.out.println("RecList successfully loaded.");
						}
						catch (FileNotFoundException exception)
						{
							System.out.println("There was an error saving the list that was loaded into the system.");
						}
						catch (NoSuchListException exception){System.out.println("This should not happen.");}
					}
					catch (ListNameTakenException exception)
					{
						System.out.println("The list name, " + input[3] + ", is already taken.");
					}
					catch (FileNotFoundException exception)
					{
						System.out.println("The specified file, " + input[1] + ", was not found.");
					}
					catch (IOException exception)
					{
						System.out.println("There was an error reading the file " + input[1]);
					}
					
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + editor + " may use this command.");
			}
			else if (input.length == 4 && input[0].equals("mergerec"))
			{
				if (user.getType().equals(verifier))
				{
					RecDatabase recDatabase = null;
					try
					{
						recDatabase = masterRecDatabase.getRecDatabase(input[3]);
					}
					catch (NoSuchListException exception)
					{
						masterRecDatabase.addRecDatabase(input[3]);
						try
						{
							recDatabase = masterRecDatabase.getRecDatabase(input[3]);
							recDatabase.addHistory("RecDatabase was created.", user.getName());
						}
						catch (NoSuchListException exception1){}
					}
					if (recDatabase != null)
					{
						try
						{	
							if (recListManager.getRecList(input[1]).getVerifier() != null)
							{
								recDatabase.merge(recListManager.getRecList(input[1]).getMovieList());
								try
								{
									recDatabase.addHistory("The recList " + input[1] + " was merged with this database.", user.getName());
									recDatabase.update(masterRecDatabaseDir);
									System.out.println("RecList successfully merged with RecDatabase.");
								}
								catch (IOException exception)
								{
									System.out.println("There was an error saving the database after its contents were modified.");
								}
							}
							else
							{
								System.out.println("The RecList " + input[1] + " has not yet been verified.");
							}
						}
						catch (NoSuchListException exception)
						{
							System.out.println("The specified RecList, " + input[1] + " does not exist.");
						}
					}
					try
					{
						recDatabase.update(masterRecDatabaseDir);
					}
					catch (IOException exception)
					{
						System.out.println("There was an error saving the changes made to the " + input[3] + " database.");
					}
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + verifier + " may use this command.");
			}
			else if (input.length == 1 && input[0].equals("listrecs"))
			{
				if (user.getType().equals(editor) || user.getType().equals(verifier))
				{
					if (recListManager.getRecLists().size() == 0)
						System.out.println("There are currently no RecLists loaded into the system.");
					else
					{
						System.out.println("RecLists currently loaded into the system:\n");
						for (RecList recList : recListManager.getRecLists())
						{
							System.out.println(recList.getName());
						}
					}
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + editor + " or " + verifier + " may use this command.");
			}
			else if (input.length == 1 && input[0].equals("listdbs"))
			{
				if (user.getType().equals(verifier) || user.getType().equals(browser))
				{
					if (masterRecDatabase.getRecDatabases().size() == 0)
						System.out.println("There are currently no Recommendation Databases.");
					else
					{
						System.out.println("Databases currently stored in the system:\n");
						for (RecDatabase recDatabase : masterRecDatabase.getRecDatabases())
						{
							System.out.println(recDatabase.getName());
						}
					}
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + verifier + " or " + browser + " may use this command.");
			}
			else if ((input.length == 2 || input.length == 3) && input[0].equals("displaydb"))
			{
				if (user.getType().equals(verifier) || user.getType().equals(browser))
				{
					if (input.length == 2)
					{
						RecDatabase recDatabase = null;
						try
						{
							recDatabase = masterRecDatabase.getRecDatabase(input[1]);
						}
						catch (NoSuchListException exception)
						{
							System.out.println("The list specified does not exist in the MasterRecommendationDatabase.");
						}
							
						try
						{
							if (recDatabase != null)
								System.out.println(recDatabase.display());
						}
						catch (EmptyListException exception)
						{
							System.out.println("The specified RecommendationDatabase is empty.");
						}
					}
					else if (input.length ==3)
					{
						RecDatabase recDatabase = null;
						try
						{
							recDatabase = masterRecDatabase.getRecDatabase(input[2]);
						}
						catch (NoSuchListException exception)
						{
							System.out.println("The list specified does not exist in the MasterRecommendationDatabase");
						}
						
						try
						{
							if (recDatabase != null)
								System.out.println(recDatabase.displaySorted());
						}
						catch (EmptyListException exception)
						{
							System.out.println("The specified RecommendationDatbase is empty.");
						}
					}
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + editor + " may use this command.");
			}
			else if (input.length == 2 && input[0].equals("editrec"))
			{
				if (user.getType().equals(editor))
				{
					RecList recList = null;
					
					try
					{
						recList = recListManager.getRecList(input[1]);
					}
					catch (NoSuchListException exception)
					{
						System.out.println("The list specified has not beed loaded into the system.");
					}
					

					if (recList != null)
					{
						while (true)
						{
							System.out.println(recList.display());
							System.out.println("Which movie recommendation do you want to edit? Enter the movie name followed by the movie year.");
							String movieToEdit = "";
							movieToEdit = in.nextLine();
							
							if (movieToEdit.split(" ").length < 2 || !Pattern.matches("\\d{4}+", movieToEdit.split(" ")[movieToEdit.split(" ").length - 1]))
							{
								System.out.println(movieToEdit.split(" ")[movieToEdit.split(" ").length - 1]);
								System.out.println("The movie specified was formatted incorrectly.");
							}
							else
							{
								MovieRec movie = null;
								try
								{
									movie = recList.search(movieToEdit.substring(0, movieToEdit.length() - 5), Integer.parseInt(movieToEdit.split(" ")[movieToEdit.split(" ").length - 1]));
								}
								catch (MovieNotFoundException exception)
								{
									System.out.println("The movie specified does not exist in the specified RecList.");
								}
								catch (EmptyListException exception)
								{
									System.out.println("The RecList being edited is empty.");
								}
								
								if (movie == null)
								{
									System.out.println("The movie specified was not found.");
								}
								else
								{
									System.out.println(movie.toString());
									System.out.println("Do you want to edit the title of this movie? y/n");
									if (getResponse())
									{
										System.out.println("Enter the replacement title:");
										movie.setName(in.nextLine());
									}
									System.out.println("Do you want to edit the release year of this movie? y/n");
									if (getResponse())
									{
										System.out.println("Enter the replacement release year:");
										while (true)
										{
											String year = in.nextLine();
											if (Pattern.matches("\\d{4}+", year))
											{
												movie.setReleaseYear(Integer.parseInt(year));
												break;
											}
											else
												System.out.println("The year entered was not formatted properly. Please enter a valid value.");
										}
									}
									System.out.println("Do you want to edit the genre of this movie? y/n");
									{
										if (getResponse())
										{
											System.out.println("Enter the replacement genre:");
											movie.setGenre(in.nextLine());
										}
									}
								}
							}
							System.out.println("Are you done making changes to this recList? y/n");
							if (getResponse())
								break;							
						}
						
						try
						{
							recList.addHistory("This recList was edited.", user.getName());
							recList.update(recListDir);
							System.out.println("Modifications saved successfully.");
						}
						catch (IOException exception)
						{
							System.out.println("There was an error saving the changes made to the " + recList + " recList.");
						}
					}
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + editor + " may use this command.");
			}
			else if (input.length == 2 && input[0].equals("verifyrec"))
			{
				if (user.getType().equals(verifier))
				{
					RecList recList = null;
					try
					{
						recList = recListManager.getRecList(input[1]);
					}
					catch (NoSuchListException exception)
					{
						System.out.println("The list specified does not exist in the system.");
					}
					
					if (recList != null)
					{
						recList.verify(user.getName());
						try
						{
							recList.addHistory("This recList was verified.", user.getName());
							recList.update(recListDir);
							System.out.println("RecList verified successfully.");
						}
						catch (IOException exception)
						{
							System.out.println("There was an error saving the recList after being verified.");
						}
					}
					
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + verifier + " may use this command.");
			}
			else if (input.length == 2 && input[0].equals("viewhistorydb"))
			{
				if (user.getType().equals(verifier))
				{
					try
					{
						masterRecDatabase.getRecDatabase(input[1]).viewHistory();
					}
					catch (NoSuchListException exception)
					{
						System.out.println("The database specified does not exist.");
					}
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + verifier + " may use this command.");
			}
			else if (input.length == 2 && input[0].equals("viewhistoryrec"))
			{
				if (user.getType().equals(verifier) || user.getType().equals(editor))
				{
					try
					{
						recListManager.getRecList(input[1]).viewHistory();
					}
					catch (NoSuchListException exception)
					{
						System.out.println("The RecList specified does not exist.");
					}
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + verifier + " or " + editor + " may use this command.");
			}
			else if (input.length == 1 && input[0].equals("displayplist"))
			{
				if (user.getType().equals(browser))
				{
					PersonalRecList pRecList = null;
					pRecList = user.getPersonalRecList();
					try
					{
						pRecList.display();
					}
					catch (EmptyListException exception)
					{
						System.out.println("Your personalRecList is currently empty.");
					}
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + browser + " may use this command.");
			}
			else if ((input.length == 2 || input.length == 3) && input[0].equals("displayrec"))
			{
				if (user.getType().equals(editor) || user.getType().equals(verifier))
				{
					if (input.length == 2)
					{
						try
						{
							System.out.println(recListManager.getRecList(input[1]).display());
						}
						catch (NoSuchListException exception)
						{
							System.out.println("The RecList specified does not exist.");
						}
					}
					else if (input[1].equalsIgnoreCase("sorted"))
					{
						try
						{
							System.out.println(recListManager.getRecList(input[2]).displaySorted());
						}
						catch (NoSuchListException exception)
						{
							System.out.println("The RecList specified does not exist.");
						}
					}
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + verifier + " or " + editor + " may use this command.");
			}
			else if ((input.length >= 5) && input[0].equals("searchdb"))
			{
				if (user.getType().equals(verifier) || user.getType().equals(browser))
				{
					if (input.length >= 6 && input[2].equalsIgnoreCase("for") && input[3].equalsIgnoreCase("title"))
					{
						RecDatabase recDatabase = null;
						String title = "";
						String year = "";
						
						try
						{
							recDatabase = masterRecDatabase.getRecDatabase(input[1]);
							
							title = input[4];
							for (int i = 5; i < input.length - 1; i++)
								title = title + " " + input[i];
							
							year = input[input.length - 1];
							
							if (!Pattern.matches("\\d{4}+", year))
								System.out.println("Either the movie year entered is formatted incorrectly, or no year was entered.");
							else
							{
								MovieRec movie = null;
								try
								{
									movie = recDatabase.search(title, Integer.parseInt(year));
									System.out.println(movie.toString());
									
									if (user.getType().equals(browser))
									{
										System.out.println("Would you like to add this movie to your personal movie RecList? y/n");
										if (getResponse())
										{
											try{
											user.getPersonalRecList().add(new PMovieRec(movie.getName(), movie.getReleaseYear()));
											}
											catch (DuplicateRecException exception)
											{
												System.out.println("This movie has already been added to your personal rec list.");
											}
											try
											{
												user.getPersonalRecList().update();
												System.out.println("Movie successfully added to your personal rec list.");
											}
											catch (IOException exception)
											{
												System.out.println("There was an error saving your personal RecList after the movie was added.");
											}
										}
									}
								}
								catch (DatabaseIsEmptyException exception)
								{
									System.out.println("The RecDatabse is empty.");
								}
								catch (MovieNotFoundException exception)
								{
									System.out.println("The movie specified was not found.");
								}
							}
						
						}
						catch (NoSuchListException exception)
						{
							System.out.println("The RecList specified does not exist.");
						}
					}
					else if (input.length >= 5 && input[2].equalsIgnoreCase("for") && input[3].equalsIgnoreCase("genre"))
					{
						RecDatabase recDatabase = null;
						String genrePhrase = "";
						
						try
						{
							recDatabase = masterRecDatabase.getRecDatabase(input[1]);
							genrePhrase = input[4];
							for (int i = 5; i < input.length; i++)
							{
								genrePhrase = genrePhrase + " " + input[i];
							}
							ArrayList<MovieRec> searchResults = null;
							
							try
							{
								searchResults = recDatabase.search(genrePhrase);
								
								for (MovieRec movie : searchResults)
									System.out.println(movie.toString());
								
								if (user.getType().equals(browser))
								{
									System.out.println("Would you like to add any of these movies to your personal movie RecList? y/n");
									while (true)
									{
										if (!getResponse())
											break;
										System.out.println("Specify the movie you would like to add to your personal movie RecList in the format \"title year\":");
										String movieParams = "";
										movieParams = in.nextLine();
										if (movieParams.split(" ").length >= 2 && Pattern.matches("\\d{4}+", movieParams.split(" ")[movieParams.split(" ").length - 1]))
										{
											String title = movieParams.split(" ")[0];
											for (int i = 1; i < movieParams.split(" ").length - 1; i ++)
											{
												title = title + " " + movieParams.split(" ")[i];
											}
											int year = Integer.parseInt(movieParams.split(" ")[movieParams.split(" ").length - 1]);
										
											boolean found = false;
											for (MovieRec movie : searchResults)
											{
												if (movie.getName().equalsIgnoreCase(title) && movie.getReleaseYear() == year)
												{
													found = true;
													try{
														user.getPersonalRecList().add(new PMovieRec(movie.getName(), movie.getReleaseYear()));
														System.out.println("Movie successfully added to your personal rec list.");
													}
													catch (DuplicateRecException exception)
													{
														System.out.println("That movie has already been added to your personal rec list.");
													}
													break;
												}	
											}
											if (!found)
												System.out.println("The specified Movie was not found amongst the current search results.");
											else
											{
												try
												{
													user.getPersonalRecList().update();
												}
												catch (IOException exception)
												{
													System.out.println("There was an error saving your personal rec list after the recent addition.");
												}
											}
										}
										else
											System.out.println("The parameters given to specify a movie were not in the correct format.");
										System.out.println("Would you like to continue adding movies to your personal movie RecList? y/n");
									}
								}
							}
							catch (DatabaseIsEmptyException exception)
							{
								System.out.println("The RecDatabase being searched is empty.");
							}
							catch (MovieNotFoundException exception)
							{
								System.out.println("No movies with the given genre were found in the sepcified RecDatabase.");
							}
						}
						catch (NoSuchListException exception)
						{
							System.out.println("The RecList specified does not exist.");
						}	
					}
					else
						System.out.println("The command entered does not match any available command. Enter 'help' for a list of avaiable commands for your user type.");
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + verifier + " or " + browser + " may use this command.");
			}
			else if ((input.length >= 5) && input[0].equals("searchrec"))
			{
				if (user.getType().equals(verifier) || user.getType().equals(editor))
				{
					if (input.length >= 6 && input[2].equalsIgnoreCase("for") && input[3].equalsIgnoreCase("title"))
					{
						RecList recList = null;
						String title = "";
						String year = "";
						
						try
						{
							recList = recListManager.getRecList(input[1]);
							
							title = input[4];
							for (int i = 5; i < input.length - 1; i++)
								title = title + " " + input[i];

							year = input[input.length - 1];							
							if (!Pattern.matches("\\d{4}+", year))
								System.out.println("Either the movie year entered is formatted incorrectly, or no year was entered.");
							else
							{
								MovieRec movie = null;
								try
								{
									movie = recList.search(title, Integer.parseInt(year));
									System.out.println(movie.toString());
								}
								catch (EmptyListException exception)
								{
									System.out.println("The RecList is empty.");
								}
								catch (MovieNotFoundException exception)
								{
									System.out.println("The movie specified was not found.");
								}
							}
						
						}
						catch (NoSuchListException exception)
						{
							System.out.println("The RecList specified does not exist.");
						}
					}
					else if (input.length >= 5 && input[2].equalsIgnoreCase("for") && input[3].equalsIgnoreCase("genre"))
					{
						RecList recList = null;
						String genrePhrase = "";
						
						try
						{
							recList = recListManager.getRecList(input[1]);
							genrePhrase = input[4];
							for (int i = 5; i < input.length; i++)
							{
								genrePhrase = genrePhrase + " " + input[i];
							}
							ArrayList<MovieRec> searchResults = null;
							
							try
							{
								searchResults = recList.search(genrePhrase);
								
								for (MovieRec movie : searchResults)
									System.out.println(movie.toString());
							}
							catch (EmptyListException exception)
							{
								System.out.println("The RecList being searched is empty.");
							}
							catch (MovieNotFoundException exception)
							{
								System.out.println("No movies with the given genre were found in the sepcified RecList.");
							}
						}
						catch (NoSuchListException exception)
						{
							System.out.println("The RecList specified does not exist.");
						}	
					}
					else
						System.out.println("The command entered does not match any available command. Enter 'help' for a list of avaiable commands for your user type.");
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + editor + " may use this command.");
			}
			else if (input.length == 4 && input[0].equals("adduser"))
			{
				if (user.getType().equals(admin))
				{
					try
					{
						if (!input[1].equals(browser) && !input[1].equals(admin) && !input[1].equals(editor) && !input[1].equals(verifier))
							System.out.println("\"" + input[1] + "\" is not a valid user type.");
						else
						{
							userDatabase.addUser(input[2], input[3], input[1]);
							try
							{
								userDatabase.update();
								System.out.println("User added successfully.");
							}
							catch (FileNotFoundException exception)
							{
								System.out.println("There was an error saving the changes to the user database");
							}
						}
					}
					catch (UserNameTakenException exception)
					{
						System.out.println("The user name specified has already been taken.");
					}
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + admin + " may use this command.");
			}
			else if (input.length == 2 && input[0].equals("deluser"))
			{
				if (user.getType().equals(admin))
				{
					try
					{
						System.out.println("Are you sure you want to delete user " + input[1] + "?");
						if (getResponse())
						{
							User delUser  = userDatabase.getUser(input[1]);
							userDatabase.deleteUser(input[1]);
							try
							{
								userDatabase.update();
								System.out.println("User deleted successfully.");
								if (delUser == user)
								{
									System.out.println("You have deleted yourself and will now be logged out.");
									System.exit(0);
								}
							}
							catch(FileNotFoundException exception)
							{
								System.out.println("There was an error saving the changes to the user database.");
							}
						}
					}
					catch (NoSuchUserException exception)
					{
						System.out.println("The user specified does not exist in the user database.");
					}
					
				}
				else
					System.out.println("You do not have permission to use this command. Only users of type " + admin + " may use this command.");
			}
			else if (input.length == 1 && input[0].equals("exit"))
			{
				System.out.println("Thank you for using the IMDB system.");
				break;				
			}
			else
				System.out.println("The command entered does not match any available command. Enter 'help' for a list of avaiable commands for your user type.");
		}
	}
	
	/**
	 * Gets the user's response to a y/n question.
	 * @return true if the user entered 'y', false if 'n'
	 */
	private boolean getResponse()
	{
		Scanner in = new Scanner(System.in);
		String response = "";
		while (true)
		{
			response = in.nextLine();
			if (response.equalsIgnoreCase("Y"))
				return true;
			else if (response.equalsIgnoreCase("N"))
				return false;
			System.out.println("The given input is not a valid response. Please enter 'y' or 'n'");
		}
	}
}
